package br.com.toscano.contpedido.util;


import java.io.IOException;

import org.w3c.dom.Element;
import org.xhtmlrenderer.extend.FSImage;
import org.xhtmlrenderer.extend.ReplacedElement;
import org.xhtmlrenderer.extend.ReplacedElementFactory;
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.pdf.ITextFSImage;
import org.xhtmlrenderer.pdf.ITextImageElement;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.simple.extend.FormSubmissionListener;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Image;

public class ProfileImageReplacedElementFactory implements ReplacedElementFactory {

    private final ReplacedElementFactory superFactory;
    
    private final byte[] signature;

    public ProfileImageReplacedElementFactory(ReplacedElementFactory superFactory, byte[] signature) {
        this.superFactory = superFactory;
        this.signature = signature;
    }

    @Override
    public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockBox blockBox,
            UserAgentCallback userAgentCallback, int cssWidth, int cssHeight) {

        Element element = blockBox.getElement();
        if (element == null) {
            return null;
        }

        Element imgTag = (Element) element.getElementsByTagName("img").item(0);
        
//        String nodeName = element.getNodeName();
//        String className = element.getAttribute("class");
        if (imgTag != null) {

            try {
            	Image image = null;
        		imgTag = (Element) element.getElementsByTagName("img").item(0);
        		String imgValue = imgTag.getAttribute("src");            	
            	
        		if (signature != null) {
        			image = Image.getInstance(signature);
        		} else {
        			if (getClass().getClassLoader().getResource(imgValue) != null) {
        				image = Image.getInstance(getClass().getClassLoader().getResource(imgValue));
        			} else {
        				image = Image.getInstance(getClass().getClassLoader().getResource("img/page_signature.jpg"));
        			}
        		}
                FSImage fsImage = new ITextFSImage(image);

                if (fsImage != null) {
                    if ((cssWidth != -1) || (cssHeight != -1)) {
                        fsImage.scale(cssWidth, cssHeight);
                    }
                    return new ITextImageElement(fsImage);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (BadElementException e) {
            	e.printStackTrace();
            } 
        }

        return superFactory.createReplacedElement(layoutContext, blockBox, userAgentCallback, cssWidth, cssHeight);
    }

    @Override
    public void reset() {
        superFactory.reset();
    }

    @Override
    public void remove(Element e) {
        superFactory.remove(e);
    }

    @Override
    public void setFormSubmissionListener(FormSubmissionListener listener) {
        superFactory.setFormSubmissionListener(listener);
    }
}
